package firbi.base.display;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * Contacts overlay. Draws a pin with text over it.
 * Contains the location and name of contact.  
 * Note: This is not actually used in the current version.
 * This is for a feature that was not completed by the first release. 
 *
 */
public class ContactOverlay extends Overlay {
	private BitmapDrawable mBitmap;
	private GeoPoint mLoc;
	private String mName;
	
	public ContactOverlay(GeoPoint p, String name, Drawable drawable) {
		mLoc = p;
		mName = name;
		mBitmap = (BitmapDrawable) drawable;
	}

	/**
	 * Sets the Point and Name variables of the contact overlay 
	 * @param p: Point where contain is located
	 * @param name: Name to be displayed
	 */
	public void setContact (GeoPoint p, String name) {
		mLoc = p;
		mName = name;
	}
	
	/**
	 * Draws a pin then displays the contact's name over the pin
	 */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		int height = mBitmap.getIntrinsicHeight();
		int width = mBitmap.getIntrinsicWidth();
		
		Projection projection = mapView.getProjection();
		Point p = new Point();
		projection.toPixels(mLoc, p);
		mBitmap.setBounds(p.x-width/2, p.y-height, p.x+width/2, p.y);
		mBitmap.draw(canvas);
		
		int rectSizeX = mName.length()*8;
		int rectSizeY = 20;
		RectF rect = new RectF(0, 0, rectSizeX, rectSizeY);
		rect.offset(p.x-rectSizeX/2, p.y-rectSizeY-height);
		Paint backgroundP = new Paint();
		backgroundP.setColor(Color.WHITE);
		backgroundP.setAntiAlias(true);
		
		Paint textP = new Paint();
		textP.setColor(Color.BLACK);
		textP.setAntiAlias(true);
		textP.setTextAlign(Paint.Align.CENTER);
		Paint boarderP = new Paint();
		
		boarderP.setColor(Color.BLACK);
		boarderP.setAntiAlias(true);
		boarderP.setStyle(Style.STROKE);
		boarderP.setStrokeWidth(2);
		
		canvas.drawRoundRect(rect, 5, 5, backgroundP);
		canvas.drawRoundRect(rect, 5, 5, boarderP);
		canvas.drawText(mName, p.x, p.y-height-rectSizeY/4, textP);
	}
}
